package ysgrpc

import (
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/text/gstr"
)

// NewMQTopicTool 构造 MQ MQTopicTool 工具类型
func NewMQTopicTool(name string) *MQTopicTool {
	// 初始化全局参数
	mq := &MQTopicTool{
		HEART:     "json.heart",
		HEART_ASK: "json.heart?",
		REQ:       "json.req",
		RES:       "json.res",
		DATA:      "json.data",
		DATA_RAW:  "json.data.raw",
		DATA_LTTB: "json.data.lttb",
		CFG:       "json.cfg",
		CFG_ASK:   "json.cfg?",
		LOG:       "str.log",
	}
	mq.init(name)
	return mq
}

type MQTopicTool struct {
	HEART     string
	HEART_ASK string

	REQ string
	RES string

	DATA      string
	DATA_RAW  string
	DATA_LTTB string

	CFG     string
	CFG_ASK string

	LOG string

	name string
}

type MQTopicInput struct {
	Base string
	Ex   string
	Name string
}

func (t *MQTopicTool) init(name string) {
	t.SetName(name)
}

func (t *MQTopicTool) Name() string {
	return t.name
}

func (t *MQTopicTool) SetName(name string) {
	if g.IsEmpty(name) {
		name = "mq-model"
	}

	t.name = name
}

func (t *MQTopicTool) GenTopic(in MQTopicInput) string {
	if g.IsEmpty(in.Name) {
		in.Name = t.Name()
	}

	arr := []string{in.Base, in.Name}
	if !g.IsEmpty(in.Ex) {
		arr = append(arr, in.Ex)
	}

	return gstr.Join(arr, ".")
}

func (t *MQTopicTool) GenHeartTopic(in MQTopicInput) string {
	in.Base = t.HEART
	return t.GenTopic(in)
}

func (t *MQTopicTool) GenReqTopic(in MQTopicInput) string {
	in.Base = t.REQ
	return t.GenTopic(in)
}

func (t *MQTopicTool) GenResTopic(in MQTopicInput) string {
	in.Base = t.RES
	return t.GenTopic(in)
}

func (t *MQTopicTool) GenDataTopic(in MQTopicInput) string {
	in.Base = t.DATA
	return t.GenTopic(in)
}

func (t *MQTopicTool) GenDataRawTopic(in MQTopicInput) string {
	in.Base = t.DATA_RAW
	return t.GenTopic(in)
}

func (t *MQTopicTool) GenDataLttbTopic(in MQTopicInput) string {
	in.Base = t.DATA_LTTB
	return t.GenTopic(in)
}

func (t *MQTopicTool) GenCfgTopic(in MQTopicInput) string {
	in.Base = t.CFG
	return t.GenTopic(in)
}

func (t *MQTopicTool) GenCfgAskTopic(in MQTopicInput) string {
	if g.IsEmpty(in.Name) {
		in.Name = t.Name()
	}

	return t.CFG_ASK + in.Name
}

func (t *MQTopicTool) GenLogTopic(in MQTopicInput) string {
	in.Base = t.LOG
	return t.GenTopic(in)
}
